package com.ylr.blog.framework.controller.web;

import com.ylr.base.common.annotation.Resubmit;
import com.ylr.blog.common.dto.blog_space.BlogSpaceDto;
import com.ylr.base.common.jsr303.group.Save;
import com.ylr.base.common.jsr303.group.Update;
import com.ylr.base.common.tool.Result;
import com.ylr.blog.common.vo.blog_space.BlogSpaceVo;
import com.ylr.blog.framework.service.BlogSpaceService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-05-18 21:39:53
 * className: BlogSpaceWebController
 * version: 1.0
 * description:
 */
@Validated
@RestController
@RequestMapping("/web/blog-space")
@AllArgsConstructor
public class BlogSpaceWebController {

    private static final Logger log = LoggerFactory.getLogger(BlogSpaceWebController.class);

    private final BlogSpaceService blogSpaceService;

    /**
     * 保存用户博客空间接口
     * @param blogSpaceVo 参数对象
     */
    @Resubmit(spEL = "'saveBlogSpace'.concat(#userPo.id)")
    @PostMapping("/save")
    public Result<Object> saveBlogSpace(@Validated(Save.class) @RequestBody BlogSpaceVo blogSpaceVo) {
        log.info("web => 开始保存用户博客空间操作。{}", blogSpaceVo);

        blogSpaceService.saveBlogSpace(blogSpaceVo);

        log.info("web => 保存用户博客空间操作结束");
        return Result.success();
    }

    /**
     * 获取博客标签接口
     * 用户获取自己的博客空间
     * @return 博客标签列表
     */
    @GetMapping("/list")
    public Result<List<BlogSpaceDto>> getBlogSpaceList() {
        log.info("web => 开始获取博客标签操作");

        List<BlogSpaceDto> blogSpaceList = blogSpaceService.getBlogSpaceList();

        log.info("web => 获取博客标签操作结束");
        return Result.success(blogSpaceList);
    }

    /**
     * 获取博客标签接口
     * 用户获取自己的博客空间
     * @return 博客标签对象
     */
    @GetMapping("/get")
    public Result<BlogSpaceDto> getBlogSpace(@RequestParam("id") Long id) {
        log.info("web => 开始获取博客标签操作。id={}", id);

        BlogSpaceDto blogSpaceDto = blogSpaceService.getBlogSpace(id);

        log.info("web => 获取博客标签操作结束");
        return Result.success(blogSpaceDto);
    }

    /**
     * 获取下一个排序等级接口
     * @param parentId 父级空间主键
     * @return 排序等级值
     */
    @GetMapping("/next/sort")
    public Result<Integer> getNextSort(@RequestParam("parentId") Long parentId) {
        log.info("web => 开始获取下一个排序等级操作。parentId={}", parentId);

        Integer nextSort = blogSpaceService.getNextSort(parentId);

        log.info("web => 获取下一个排序等级操作结束");
        return Result.success(nextSort);
    }

    /**
     * 修改博客空间接口
     * 用户修改自己的博客空间
     * @param blogSpaceVo 参数对象
     */
    @Resubmit(spEL = "'updateBlogSpace'.concat(#userPo.id)")
    @PostMapping("/update")
    public Result<Object> updateBlogSpace(@Validated(Update.class) @RequestBody BlogSpaceVo blogSpaceVo) {
        log.info("web => 开始修改博客空间操作。{}", blogSpaceVo);

        blogSpaceService.updateBlogSpace(blogSpaceVo);

        log.info("web => 修改博客空间操作结束");
        return Result.success();
    }

    /**
     * 删除博客空间接口
     * 用户删除自己的博客空间
     * @param id 空间主键
     */
    @Resubmit(spEL = "'deleteBlogSpace'.concat(#userPo.id)")
    @DeleteMapping("/delete")
    public Result<Object> deleteBlogSpace(@RequestParam("id") Long id) {
        log.info("web => 开始删除博客空间操作。id={}", id);

        blogSpaceService.deleteBlogSpace(id);

        log.info("web => 删除博客空间操作结束");
        return Result.success();
    }

}
